package cn.hpclub.server.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

public class UedtiorConfigUtil{
    public static final String CONFIG_FILE_NAME = "ueditor_config.json";// 系统配置文件名称
    
    /**
     * 获取系统配置信息
     * 
     * @return SystemConfig对象
     */
    public static String getUeditorConfig() {
        try{
            String configFilePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath() + CONFIG_FILE_NAME;
            return readFile(configFilePath);
        }
        catch(URISyntaxException e){
            e.printStackTrace();
        }
        catch(IOException e){
            e.printStackTrace();
        }
        return "";
    }
    
    private static String readFile ( String path ) throws IOException {
        
        StringBuffer builder = new StringBuffer();
        
        try {
            
            InputStreamReader reader = new InputStreamReader( new FileInputStream( path ), "UTF-8" );
            BufferedReader bfReader = new BufferedReader( reader );
            
            String tmpContent = null;
            
            while ( ( tmpContent = bfReader.readLine() ) != null ) {
                builder.append( tmpContent );
            }
            
            bfReader.close();
            
        } catch ( UnsupportedEncodingException e ) {
            // 忽略
        }
        
        return filter( builder.toString() );
        
    }
    
    // 过滤输入字符串, 剔除多行注释以及替换掉反斜杠
    private static String filter ( String input ) {
        
        return input.replaceAll( "/\\*[\\s\\S]*?\\*/", "" );
        
    }
}
